<?php
class Weixinapi extends Base_Controller
{
	private $_access_token="";
	private $_appid="";
	private $_appsecret="";
	
	public function __construct()
	{
		parent::__construct();
		//微信订阅号
 		$this->_appid="wx10cf4a129eb196fe";
 		$this->_appsecret="eede380fbbb23300924d4b151d815426";
        //微信测试号
		//$this->_appid="wxca8dbf6de7842083";
		//$this->_appsecret="d4624c36b6795d1d99dcf0547af5443d";
	}
	
	//查询菜单设置
    public function get_menu_info()
    {
    	//echo $this->_appid."<br>".$this->_appsecret;
    	$this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>'dingyu'));
    	$res=$this->wxinterface->get_menu_info();
    	$this->success($res);
    }
    
    //设置菜单
    public function set_menu()
    {
    	$button='{
	    		"button":[
		            {
    			        "type":"view",
    			        "name":"定制案例",
    			        "url":"http://m.wayatrip.com/designtrip/index.html?anli/anli_index.html"
    			        
		                
		            },
		            {
		                "name":"城市探秘",
    			        "sub_button":[
    			             {
                               "type":"view",
    			               "name":"情报雷达",
    			               "url":"http://m.wayatrip.com/treasure_hunter/radar/index.html"
                             },
    	                     {
                               "type":"click",
    			               "name":"本周活动",
    			               "key":"treasure_hunter_act"
                             },
    			             {
                               "type":"view",
    			               "name":"我要报名",
    			               "url":"http://api.wayatrip.com/ucenter/weixinlogin?page=treasure_hunter/baomingpages/index.html"
                             }
    	                 ]
		            },
		            {
    			        "name":"哇吖服务",
    			        "sub_button":[
    	                     {
                               "type":"view",
				                "name":"哇吖社区",
				                "url":"http://community.wayatrip.com/plugin.php?id=wechat:access"
                             },
    			             {
                                "type":"click",
				                "name":"哇吖客服",
				                "key":"b001_wayakefu"
                             }
    	                 ]
		                
		            }
	            ]
    			}';
    	$this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>'dingyu'));
    	$res=$this->wxinterface->set_menu($button);
    	$this->success($res);    	
    }
    
    //被动回复消息
    public function response_msg()
    {
    	$this->load->library('wxinterface/Wxcallbackapi');
    	if (!isset($_GET['echostr'])) {
    		$result=$this->wxcallbackapi->responseMsg();
    		echo $result;
    		exit;
    	}else{
    		$this->wxcallbackapi->valid();
    	}
    }
    
    //js sdk初始化验证
    public function init_wx_jsapi()
    {
    	if(isset($_GET['from_url'])){
    		$from_url = $_GET['from_url'];
    	}else{
    		$this->error('缺少参数from_url');
    	}
    	
    	$this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>'dingyu'));
    	//签名
    	$init['signature'] = $this->wxinterface->getJsapiSignature($from_url);
    	//签名使用的随机串
    	$init['nonceStr'] = $this->wxinterface->getJsapiNonceStr();
    	//签名使用的时间戳
    	$init['timestamp'] = $this->wxinterface->getJsapiTimestamp();
    	 
    	$init['appid'] = $this->wxinterface->getAppid();
    	 
    	$this->success($init);
    }
    
    //查询已在微信注册ibeacon设备列表
    public function wx_device_search()
    {
    	$data = '{
				    "type": 2,
				    "last_seen": 0,		
				    "count": 10
                 }';
    	
    	$this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>'dingyu'));
    	$res=$this->wxinterface->wx_device_search($data);
    	//返回示例
    	/*{
    	 "data": {
	    	"devices": [
		    	{
		    	"comment": "",
		    	"device_id": 10097,
		    	"major": 10001,
		    	"minor": 12102,
		    	"status": 1,
		    	"last_active_time":1437276018,
		    	"poi_id": 0,
		    	"uuid": "FDA50693-A4E2-4FB1-AFCF-C6EB07647825"
		    	},
		    	.....
		     ],
	    	"total_count": 151
	      },
    	  "errcode": 0,
    	  "errmsg": "success."
    	}*/
    	var_dump($res);
    }
    
    //新增微信注册ibeacon设备分组
    public function add_wx_device_group()
    {
    	if(isset($_POST['group_name'])){
    		$name = $_POST['group_name'];
    	}else{
    		$this->error('缺少分组名称参数');
    	}
    	//var_dump($name);exit;
    	$data = array('group_name'=>$name);
    	$data = json_encode($data);
    	
    	$this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>'dingyu'));
    	$res=$this->wxinterface->add_wx_device_group($data);
    	//返回示例
    	/*{
			  "data": {
			      "group_id" : 123,
			      "group_name" : "test"
			  },
			  "errcode": 0,
			  "errmsg": "success."
		}*/
    	$this->success($res);
    }
    
    //修改微信注册ibeacon设备分组名
    public function edit_wx_device_group_name()
    {
    	if(isset($_POST['group_name']) && isset($_POST['group_id'])){
    		$name = $_POST['group_name'];
    		$id   = $_POST['group_id']; 
    	}else{
    		$this->error('缺少分组名称参数');
    	}
    	
    	$data = array('group_name'=>$name,'group_id'=>(int)$id);
    	$data = json_encode($data);
    	/*请求示例
    	 {
		    "group_id":123456,
		    "group_name":"test"
	     }*/
    	
    	$this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>'dingyu'));
    	$res=$this->wxinterface->edit_wx_device_group_name($data);
    	//返回示例
    	/*{
    		"data": {
    		},
    		"errcode": 0,
    		"errmsg": "success."
        }*/
    	//var_dump($res);
    	$this->success($res);
    }
    
    //删除修改微信注册ibeacon设备分组
    public function delete_wx_device_group()
    {
    	if(isset($_POST['group_id'])){
    		$id   = $_POST['group_id'];
    	}else{
    		$this->error('缺少分组ID参数');
    	}
    	
    	$data = array('group_id' => (int)$id);
    	$data = json_encode($data);
    	/*请求示例
    	 {
		   "group_id":123456
		 }*/
    	
    	$this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>'dingyu'));
    	$res=$this->wxinterface->delete_wx_device_group($data);
    	//返回示例
    	/*{
    		"data": {
    		},
    		"errcode": 0,
    		"errmsg": "success."
         }*/
    	//var_dump($res);
    	$this->success($res);
    }
    
    //从分组中移除设备
    public function delete_wx_device_from_group()
    {
    	//注意数据类型转换
    	if(isset($_POST['group_id']) && isset($_POST['device_id'])
    	&& isset($_POST['uuid']) && isset($_POST['major']) && isset($_POST['minor']))
    	{
    		$grpid = (int)$_POST['group_id'];
    		$devid = (int)$_POST['device_id'];
    		$uuid  = $_POST['uuid'];
    		$major = (int)$_POST['major'];
    		$minor = (int)$_POST['minor'];
    	}else{
    		$this->error('参数不全');
    	}
    	
    	//注意数据类型转换
    	$data = array('group_id'=>$grpid,'device_identifiers'=>
    	              array(
    	              		array('device_id'=>$devid,'uuid'=>$uuid,'major'=>$major,'minor'=>$minor)
    	              )
    	);
    	//var_dump($data);exit;
    	$data = json_encode($data);
    	//请求示例
    	/*{
			  "group_id": 2569121,
			  "device_identifiers":[
		          {
		          "device_id":8224142,	
		          "uuid":"FDA50693-A4E2-4FB1-AFCF-C6EB07647825",	
		          "major":10090,
		          "minor":4460
		          }
			   ]
		   }*/
    	
    	$this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>'dingyu'));
    	$res=$this->wxinterface->delete_wx_device_from_group($data);
    	//返回示例
    	/*{
    		"data": {
    		},
    		"errcode": 0,
    		"errmsg": "success."
         }*/
    	$this->success($res);
    }
    //新增微信注册ibeacon设备到分组
    public function add_wx_device_to_group()
    {
    	//注意数据类型转换
    	if(isset($_POST['group_id']) && isset($_POST['device_id']) 
    	   && isset($_POST['uuid']) && isset($_POST['major']) && isset($_POST['minor']))
    	{
    		$grpid = (int)$_POST['group_id'];
    		$devid = (int)$_POST['device_id'];
    		$uuid  = $_POST['uuid'];
    		$major = (int)$_POST['major'];
    		$minor = (int)$_POST['minor'];
    	}else{
    		$this->error('参数不全');
    	}
    	
    	$data = array('group_id'=>$grpid,'device_identifiers'=>
    	              array(
    	              		array('device_id'=>$devid,'uuid'=>$uuid,'major'=>$major,'minor'=>$minor)
    	              )
    	);

    	$data = json_encode($data);
    	//请求示例
    	/*{
			  "group_id": 2569121,
			  "device_identifiers":[
		          {
		          "device_id":8224142,	
		          "uuid":"FDA50693-A4E2-4FB1-AFCF-C6EB07647825",	
		          "major":10090,
		          "minor":4460
		          }
			   ]
		   }*/
    	
    	$this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>'dingyu'));
    	$res=$this->wxinterface->add_wx_device_to_group($data);
    	//返回示例
    	/*{
    		"data": {
    		},
    		"errcode": 0,
    		"errmsg": "success."
         }*/
    	$this->success($res);
    }
    //查询微信注册ibeacon设备分组列表
    public function wx_device_group_list()
    {
    	$data = '{
    			   "begin": 0,
                   "count": 10
                 }';
    	
    	$this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>'dingyu'));
    	$res=$this->wxinterface->wx_device_group_list($data);
    	//返回示例
    	/*{
    		"data": {
	    		"total_count": 100,
	    		"groups":[
		    		{
		    		   "group_id" : 123,
		    		   "group_name" : "test1"
		            },
		    		....
		        ]
	         },
    	    "errcode": 0,
    		"errmsg": "success."
         }*/
    	if($res['errcode'] == 0){
    		echo "total_count => ".$res['data']['total_count']."<br>";
    		foreach ($res['data']['groups'] as $k=>$v){
    			echo "<hr>group".$k."<br>group_id => ".$v['group_id']."<br>group_name => ".$v['group_name'];
    		}
    	}else{
    		echo "errcode => ".$res['errcode']."<br>errmsg => ".$res['errmsg'];
    	}
    	
    	//var_dump($res);
    }
    //查询微信注册ibeacon设备分组详情
    public function wx_device_group_detail()
    {
    	if(isset($_GET['grpid'])){
    		$grpid = (int)$_GET['grpid'];
    	}else{
    		$this->error('缺少设备组id');
    	}
    	$data = array('group_id'=>$grpid,'begin'=>0,'count'=>10);
    	$data = json_encode($data);
    	//请求示例
    	/*{
		  "group_id":2569121,
		  "begin":0,
		  "count":10
		}*/
    	$this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>'dingyu'));
    	$res=$this->wxinterface->wx_device_group_detail($data);
    	//返回示例
    	/*{
			  "data": {
			      "group_id" : 123,
			      "group_name" : "test",
			      "total_count": 100,
			      "devices" :[
			          {
			          "device_id" : 123456,
			          "uuid" : "FDA50693-A4E2-4FB1-AFCF-C6EB07647825",
			          "major" : 10001,
			          "minor" : 10001,
			          "comment" : "test device1",
			          "poi_id" : 12345,
			          },
			          {
			          "device_id" : 123457,
			          "uuid" : "FDA50693-A4E2-4FB1-AFCF-C6EB07647825",
			          "major" : 10001,
			          "minor" : 10002,
			          "comment" : "test device2",
			          "poi_id" : 12345,
			          }
			          .....
			          ]
			     	},
			  "errcode": 0,
			  "errmsg": "success."
           }*/
    	if($res['errcode'] == 0){
    		echo "group_id => ".$res['data']['group_id']."<br>";
    		echo "group_name => ".$res['data']['group_name']."<br>";
    		echo "total_count => ".$res['data']['total_count']."<br>";
    		foreach ($res['data']['devices'] as $k=>$v){
    			echo "<hr>device".$k
    			     ."<br>device_id => ".$v['device_id']
    			     ."<br>uuid => ".$v['uuid']
    			     ."<br>major => ".$v['major']
    			     ."<br>minor => ".$v['minor']
    			     ."<br>comment => ".$v['comment']
    			     ."<br>poi_id => ".$v['poi_id'];
    		}
    	}else{
    		echo "errcode => ".$res['errcode']."<br>errmsg => ".$res['errmsg'];
    	}
    }
    
    
}